Imports System.Web.Services
Imports System.IO
Imports System.Net
Imports System.Text

<WebService(Namespace:="http://ccec.net/services/")> _
Public Class Scrape
    Inherits System.Web.Services.WebService

#Region " Web Services Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Web Services Designer.
        InitializeComponent()

        'Add your own initialization code after the InitializeComponent() call

    End Sub

    'Required by the Web Services Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Web Services Designer
    'It can be modified using the Web Services Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        'CODEGEN: This procedure is required by the Web Services Designer
        'Do not modify it using the code editor.
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

#End Region

    <WebMethod()> Public Function SnoScrape() As Boolean

        Dim encASCIIEncoding As New ASCIIEncoding
        Dim wcWebClient As New WebClient
        Dim strText As String
        Dim iCRLoc As Integer

        Try
            'Make GET request to URL, convert the returned BYTE array to a string
            strText = encASCIIEncoding.GetString(wcWebClient.DownloadData("http://waterdata.usgs.gov/wa/nwis/uv?dd_cd=01&dd_cd=02&format=rdb&period=1&site_no=12149000"))
            iCRLoc = InStrRev(strText, "USGS")
            strText = Right(strText, Len(strText) - iCRLoc + 1)

            'Store full string
            Application("strFullText") = strText

            'Store Agency
            Application("strAgency") = Left(strText, 4)
            strText = Right(strText, Len(strText) - 5)

            'Store SiteCode
            Application("strSiteCode") = Left(strText, 8)
            strText = Right(strText, Len(strText) - 9)

            'Parse Date
            Application("strLatestDate") = Left(strText, 10)
            strText = Right(strText, Len(strText) - 11)

            'Parse Time
            Application("strLatestTime") = Left(strText, 5)
            strText = Right(strText, Len(strText) - 6)

            'Parse Height
            Dim iSplitter As Integer
            iSplitter = InStr(strText, Chr(9))
            Application("strLatestHeight") = Left(strText, iSplitter - 1)
            strText = Right(strText, Len(strText) - iSplitter)

            'Parse Flow
            Application("strLatestFlow") = strText

            SnoScrape = True

        Catch
            'An error has occurred. Let the user know
            SnoScrape = False
        End Try
    End Function

    <WebMethod()> Public Function GetFullText() As String
        GetFullText = Application("strFullText")
    End Function

    <WebMethod()> Public Function GetAgency() As String
        GetAgency = Application("strAgency")
    End Function

    <WebMethod()> Public Function GetSiteCode() As String
        GetSiteCode = Application("strSiteCode")
    End Function

    <WebMethod()> Public Function GetLatestDate() As String
        GetLatestDate = Application("strLatestDate")
    End Function

    <WebMethod()> Public Function GetLatestTime() As String
        GetLatestTime = Application("strLatestTime")
    End Function

    <WebMethod()> Public Function GetLatestHeight() As Double
        GetLatestHeight = Val(Application("strLatestHeight"))
    End Function

    <WebMethod()> Public Function GetLatestHeightStr() As String
        GetLatestHeightStr = Application("strLatestHeight")
    End Function

    <WebMethod()> Public Function GetLatestFlow() As Double
        GetLatestFlow = Val(Application("strLatestFlow"))
    End Function

    <WebMethod()> Public Function GetLatestFlowStr() As String
        GetLatestFlowStr = Application("strLatestFlow")
    End Function

    <WebMethod()> Public Function GetHeightPercent() As Integer
        GetHeightPercent = (GetLatestHeight() * 100 - 4500) / 9
    End Function


End Class
